<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <!-- Vue定义一个组件：
        1、定义组件 
        2、注册组件(全局注册、局部注册)
        3、使用组件

        组件之间的通信：父传子(props)、组件组件之间传递

    -->
  <!-- 准备好一个容器-->
  <div id="app">
    <Student v-bind:name="student1.name" v-bind:age="student1.age"></Student>
    <Student :name="student2.name" :age="student2.age"></Student>
    <Banji></Banji>
  </div>

  <script src="vue.js"></script>
  <script>
    Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。

    //VueComponent
    //创建学生Student组件，组件里面有html页面、data、methods
    const Student = Vue.extend({
      template: `
        <div>
          <h2>student姓名：{{name}}</h2>
          <h2>student年龄：{{age}}</h2>
          <h2>student性别：{{gender}}</h2>
          <button @click="showStudentInof()">显示student信息</button>
        </div>
      `,
      props: ['name', 'age'],//子组件通过props接收父组件传递过来的数据
      data() {
        return {
          gender: '男'
        }
      },
      methods: {
        showStudentInof() {
          alert(this.name + " : " + this.age);
        }
      }
    })

    const Banji = Vue.extend({
      template: `
        <div>
          <h2>班级姓名：{{banjiName}}</h2>
          <h2>班级人数：{{banjiCount}}</h2>
          <button @click="showBanjiInof()">显示班级信息</button>
        </div>`,
      data() {
        return{
          banjiName: 'Java230301',
          banjiCount: 49
        }
      },
      methods: {
        showBanjiInof() {
          alert(this.banjiName + " : " + this.banjiCount);
        }
      }
    });

    //创建vue实例
    new Vue({
      el: '#app',
      data: {
        student1: {
          name: '张三',
          age:23
        },
        student2: {
          name: '李四',
          age: 24
        }
      },
      components: {//注册组件：局部注册
        Student,
        Banji
      }
    })
    
  </script>
</body>

</html>